package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Service
@RequestMapping("comments")
@Slf4j
public class QuanZiCommentController {
     @Autowired
     private QuanZiService quanZiService;
    @GetMapping
    public ResponseEntity<PageResult> queryCommentlist(@RequestParam("movementId")String publishId,
                                                       @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                       @RequestParam(value = "pagesize",defaultValue = "10")Integer pagesize){
        try {
            PageResult pageResult= quanZiService.queryCommentlist(publishId,page,pagesize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String,String> parm){
        try {
            String publishId = parm.get("movementId");
            String content = parm.get("comment");
           Boolean result= quanZiService.saveComments(publishId,content);
           if(result){
             return  ResponseEntity.ok(null);
           }
        } catch (Exception e) {
            e.printStackTrace();
        }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
     @GetMapping("{id}/like")
     public ResponseEntity<Long> likeComment(@PathVariable("id")String publishId){
         try {
             Long likeCount=  quanZiService.likeComment(publishId);
             if(ObjectUtil.isNotEmpty(likeCount)){
                 return  ResponseEntity.ok(likeCount);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
     }
     @GetMapping("{id}/dislike")
     public  ResponseEntity<Long> dislikeComment(@PathVariable("id")String publishId){
         try {
             Long likeCount= quanZiService.dislikeComment(publishId);
             if(ObjectUtil.isNotEmpty(likeCount)){
                 return  ResponseEntity.ok(likeCount);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
     }
}
